package me.ingxin.android.permission2

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Environment
import android.provider.Settings
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat

internal val Context.targetSdkVersion: Int
    get() = applicationInfo.targetSdkVersion

internal fun Context.permissionGranted(permission: String): Boolean =
    ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED

/**悬浮窗权限*/
internal const val PERMISSION_SYSTEM_ALERT_WINDOW = Manifest.permission.SYSTEM_ALERT_WINDOW

/**修改系统设置*/
internal const val PERMISSION_WRITE_SETTINGS = Manifest.permission.WRITE_SETTINGS

/**安装未知应用*/
internal const val PERMISSION_REQUEST_INSTALL_PACKAGES =
    Manifest.permission.REQUEST_INSTALL_PACKAGES

/**管理所有文件夹权限*/
internal val PERMISSION_MANAGE_EXTERNAL_STORAGE
    get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        Manifest.permission.MANAGE_EXTERNAL_STORAGE
    } else {
        "android.permission.MANAGE_EXTERNAL_STORAGE"
    }

/**后台定位权限*/
internal val PERMISSION_ACCESS_BACKGROUND_LOCATION
    get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        Manifest.permission.ACCESS_BACKGROUND_LOCATION
    } else {
        "android.permission.ACCESS_BACKGROUND_LOCATION"
    }

/**通知权限，低版本也有需要跳转打开通知的需求，所以开放该属性*/
val PERMISSION_POST_NOTIFICATIONS = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    Manifest.permission.POST_NOTIFICATIONS
} else {
    "android.permission.POST_NOTIFICATIONS"
}

/**后台访问身体传感器数据*/
internal val BODY_SENSORS_BACKGROUND = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    Manifest.permission.BODY_SENSORS_BACKGROUND
} else {
    "android.permission.BODY_SENSORS_BACKGROUND"
}

/**框架支持的特殊权限*/
internal val supportSpecialPermissions = setOf(
    PERMISSION_ACCESS_BACKGROUND_LOCATION,
    PERMISSION_MANAGE_EXTERNAL_STORAGE,
    PERMISSION_SYSTEM_ALERT_WINDOW,
    PERMISSION_WRITE_SETTINGS,
    PERMISSION_REQUEST_INSTALL_PACKAGES,
    PERMISSION_POST_NOTIFICATIONS,
    BODY_SENSORS_BACKGROUND
)

internal fun getSupportSpecialPermissionName(context: Context, permission: String): String {
    return when (permission) {
        PERMISSION_ACCESS_BACKGROUND_LOCATION -> context.getString(R.string.px_access_background_location)
        PERMISSION_MANAGE_EXTERNAL_STORAGE -> context.getString(R.string.px_manage_external_storage)
        PERMISSION_SYSTEM_ALERT_WINDOW -> context.getString(R.string.px_system_alert_window)
        PERMISSION_WRITE_SETTINGS -> context.getString(R.string.px_write_settings)
        PERMISSION_REQUEST_INSTALL_PACKAGES -> context.getString(R.string.px_request_install_packages)
        PERMISSION_POST_NOTIFICATIONS -> context.getString(R.string.px_post_notification)
        BODY_SENSORS_BACKGROUND -> context.getString(R.string.px_body_sensor_background)
        else -> throw IllegalArgumentException("未适配权限 $permission")
    }
}

internal fun Context.specialPermissionGranted(permission: String): Boolean {
    return when (permission) {
        PERMISSION_ACCESS_BACKGROUND_LOCATION -> backgroundLocation(this)
        PERMISSION_MANAGE_EXTERNAL_STORAGE -> manageExternalStorage()
        PERMISSION_SYSTEM_ALERT_WINDOW -> alertWindow(this)
        PERMISSION_WRITE_SETTINGS -> writeSettingsGranted(this)
        PERMISSION_REQUEST_INSTALL_PACKAGES -> installPackagesGranted(this)
        PERMISSION_POST_NOTIFICATIONS -> notificationsEnabled(this)
        BODY_SENSORS_BACKGROUND -> backgroundBodySensors(this)
        else -> false
    }
}

private fun installPackagesGranted(context: Context): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && context.targetSdkVersion >= Build.VERSION_CODES.O) {
        context.packageManager.canRequestPackageInstalls()
    } else {
        //低版本没有该权限定义
        false
    }
}

private fun writeSettingsGranted(context: Context): Boolean {
    return if (context.targetSdkVersion >= Build.VERSION_CODES.M) {
        Settings.System.canWrite(context)
    } else {
        //低版本不需要申请,可以直接使用
        true
    }
}

private fun alertWindow(context: Context): Boolean {
    return if (context.targetSdkVersion >= Build.VERSION_CODES.M) {
        Settings.canDrawOverlays(context)
    } else {
        //低版本不需要申请
        true
    }
}

private fun manageExternalStorage(): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        Environment.isExternalStorageManager()
    } else {
        //低版本没有该权限定义
        false
    }
}

/**
 * “仅使用期间运行”-表示本次拒绝，下次申请任然会显示系统权限框
 * “仅使用期间运行且不在询问”-表示本次拒绝，以后申请都不会显示系统弹窗且权限直接申请失败
 * “始终运行”-用户同意授权
 */
private fun backgroundLocation(context: Context): Boolean {
    //android10
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        context.permissionGranted(PERMISSION_ACCESS_BACKGROUND_LOCATION)
    } else {
        //低版本没有该权限定义
        false
    }
}

private fun backgroundBodySensors(context: Context): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        context.permissionGranted(BODY_SENSORS_BACKGROUND)
    } else {
        false
    }
}

private fun notificationsEnabled(context: Context): Boolean {
    return NotificationManagerCompat.from(context).areNotificationsEnabled()
}